<template>
  <el-row>
    <el-card>
      <el-form ref="searchForm" :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item :label="$t('finance.report.accountMonitoring.platformName')">
          <el-input v-model="searchForm.platformName" clearable :placeholder="$t('collectionManagement.caseManagement.prompt.orderNo')" :maxlength="50" />
        </el-form-item>
        <el-row>
          <el-button type="primary" @click="search">
            {{ $t('common.search') }}
          </el-button>
          <el-button @click="reset">
            {{ $t('common.reset') }}
          </el-button>
          <el-button :disabled="!(totalNumber > 0)" @click="exportList">
            {{ $t('common.ex') }}
          </el-button>
        </el-row>
      </el-form>
    </el-card>
    <el-card>
      <el-table
        v-loading="loading"
        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
        :data="tableData"
        border
        :stripe="true"
        style="width: 100%;"
      >
        <el-table-column
          prop="platformName"
          :label="tableText['paymentPlatform']"
          min-width="100"
        />
        <el-table-column
          prop="balance"
          :label="tableText['accountBalance']"
          min-width="150"
        />
        <el-table-column
          prop="effectiveBalance"
          :label="tableText['withdrawableBalance']"
          min-width="150"
        />
        <el-table-column
          prop="effectiveBalanceLimit"
          :label="tableText['balanceLimitTB']"
          min-width="200"
        />
        <el-table-column
          prop="email"
          :label="tableText['notification']"
          min-width="200"
        />
        <el-table-column
          prop="status"
          :label="tableText['st']"
          min-width="100"
        >
          <template slot-scope="scope">
            {{ setValue(scope.row['status']) }}
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          :label="$t('common.operate.name')"
          min-width="100"
        >
          <template slot-scope="scope">
            <el-button size="small" @click="editItem(scope.row)">{{ $t('common.operate.edit') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :current-page="searchForm.currentPage"
          :page-sizes="[10, 20, 30, 40, 50]"
          :page-size="searchForm.pageSize"
          :total="totalNumber"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <account-monitoring-dialog
      :show-dialog="editDialog"
      :choose-item="chooseItem"
      @change-dialog="changeDialog"
      @edit-success="editSuccess"
    />
  </el-row>
</template>

<script>
import { getAccountMonitoring, exportAccountMonitoring } from '@/api/finance'
import AccountMonitoringDialog from '@/components/finance/account-monitoring-dialog'
export default {
  name: 'AccountMonitoring',
  components: {
    AccountMonitoringDialog
  },
  data() {
    return {
      loading: false,
      editDialog: false,
      tableData: [],
      tableText: {},
      totalNumber: 0,
      searchForm: this.params(),
      chooseItem: {
        id: null,
        platformName: null,
        effectiveBalanceLimit: null,
        emailq: null,
        emailh: null,
        email: null,
        paymentFlag: null
      }
    }
  },
  mounted() {
    this.tableText = this.$t('finance.report.accountMonitoring')
    this.getData()
  },
  methods: {
    params() {
      return {
        currentPage: 1,
        pageSize: 10,
        platformName: null
      }
    },
    search() {
      this.searchForm.currentPage = 1
      this.getData()
    },
    reset() {
      this.searchForm = this.params()
      this.getData()
    },
    getData() {
      this.loading = true
      getAccountMonitoring(this.searchForm)
        .then(res => {
          this.loading = false
          if (res.success) {
            this.tableData = res.data
            this.totalNumber = res.page ? res.page['totalNumber'] : 0
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error(err.message)
        })
    },
    exportList() {
      exportAccountMonitoring(this.searchData())
    },
    setValue(val) {
      const name = this.$t('finance.report.accountMonitoring.status').filter(item => {
        return item.value === val
      })
      return (name && name[0] && name[0].desc) ? name[0].desc : ''
    },
    handleSizeChange(val) {
      this.searchForm.pageSize = val
      this.getData()
    },
    handleCurrentChange(val) {
      this.searchForm.currentPage = val
      this.getData()
    },
    editItem(item) {
      this.choose(item)
      this.editDialog = true
    },
    choose(param) {
      this.chooseItem.id = param.id
      this.chooseItem.platformName = param.platformName
      this.chooseItem.effectiveBalanceLimit = param.effectiveBalanceLimit
      this.chooseItem.emailq = param.email ? param.email.split('@')[0] : ''
      this.chooseItem.emailh = param.email ? '@' + param.email.split('@')[1] : ''
      this.chooseItem.email = param.email
      this.chooseItem.paymentFlag = param.paymentFlag
    },
    editSuccess() {
      this.getData()
    },
    changeDialog(param) {
      this.editDialog = param
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card{
  margin: 10px;
}
</style>
